package com.mfz.users

import com.mfz.common.persistence.AbstractFactory
import com.mfz.common.persistence.AbstractPersistence
import com.mfz.common.units.ID
import com.mfz.users.definition.COMPANY_ID

/**
 * 权限对象的实体类
 */
class Permission private constructor() : AbstractPersistence(Companion), com.mfz.common.definition.organization.Permission {
	private lateinit var _ownerCompany: Company

	companion object : AbstractFactory<Permission>("permission") {
		init {
			useBuffer = true

			//此动作从数据库当中取出所有的权限对象并且初始化
			getPersistences(" 1 = 1")
		}

		override fun createNewObject(): Permission {
			return Permission()
		}

		fun loadCompanyPermissions(companyId: Int): List<Permission> {
			return getPersistences(" $COMPANY_ID = $companyId")
		}
	}

	override val ownerCompany: Company
		get() {
			if (!this::_ownerCompany.isInitialized) {
				_ownerCompany = Company.get(ID, get(COMPANY_ID, -1))!!
			}

			return _ownerCompany
		}
}
